package com.joysuch.wwyt.streampusher;

import com.joysuch.wwyt.core.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author jiahao
 * @date 2020/7/30 19
 */
@Slf4j
@Component
public class JsStreamPusherRedisSubscriber extends JsAbstractRedisSubscriber {

    public JsStreamPusherRedisSubscriber() {
        super(Constants.STREAM_REDIS_TOPIC);
    }

    @Autowired
    private StreamPusherManager streamPusherManager;
    private static String suffix = "_wwyt";
    @Override
    public void onMessage(CharSequence channel, Object msg) {
        if (msg == null) {
            return;
        }
        log.info("stream breath sync {}", msg);
        String message = (String) msg;
        message=message.replace(suffix,"");
        String[] items = message.split("_");
        String type = items[0];
        String value = message.replace(type + "_", "");
        // 处理消息
        if ("breath".equals(type)) {
            streamPusherManager.breathFromOtherServerRequest(value);
        } else if ("close".equals(type)) {
            streamPusherManager.closeFromOtherServerRequest(value);
        }

    }
}
